package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.StockTakingCheck;
import com.woniu.supermarket.mapper.StockTakingCheckMapper;
import com.woniu.supermarket.mapper.StockTakingMapper;
import com.woniu.supermarket.service.IStockTakingService;
import com.woniu.supermarket.vo.StockTakingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StockTakingServiceImpl implements IStockTakingService {

    @Autowired
    StockTakingMapper stockTakingMapper;

    @Autowired
    StockTakingCheckMapper stockTakingCheckMapper;

    @Override
    public PageInfo<StockTakingVo> findByStockId(Integer now, Integer size,String code,Integer status) {
        PageHelper.startPage(now,size);
        List<StockTakingVo> lists = stockTakingMapper.selectByStockId(code,status);
        for(StockTakingVo s : lists){
            BigDecimal costPrice = s.getGoods().getCostPrice();//成本价
            BigDecimal retailPrice = s.getGoods().getRetailPrice();//零售价
            BigDecimal b1 = new BigDecimal(String.valueOf(costPrice));
            BigDecimal b2 = new BigDecimal(String.valueOf(retailPrice));
            BigDecimal price = b2.subtract(b1);
            int flag = price.compareTo(BigDecimal.valueOf(0));
            if(flag == -1){
                s.setLosePrice(price);
            }else if(flag == 1){
                s.setAddPrice(price);
            }
        }
        return new PageInfo<>(lists);
    }

    @Override
    public int removeStockTakingById(Integer id) {
        return stockTakingMapper.deleteStockTakingById(id);
    }

    @Override
    public List<StockTakingVo> findGoodsByStockId(Integer sid) {
        List<StockTakingVo> lists = stockTakingMapper.selectGoodsByStockId(sid);
        for(StockTakingVo s : lists){
            BigDecimal costPrice = s.getGoods().getCostPrice();//成本价
            BigDecimal retailPrice = s.getGoods().getRetailPrice();//零售价
            BigDecimal b1 = new BigDecimal(String.valueOf(costPrice));
            BigDecimal b2 = new BigDecimal(String.valueOf(retailPrice));
            BigDecimal price = b2.subtract(b1);
            int flag = price.compareTo(BigDecimal.valueOf(0));
            if(flag == -1){
                s.setLosePrice(price);
            }else if(flag == 1){
                s.setAddPrice(price);
            }
        }
        return lists;
    }

    @Override
    @Transactional
    public int addStockTakingCheck(StockTakingCheck stockTakingCheck) {
        //修改库存审核状态
        Integer status = stockTakingCheck.getCheckResult();
        System.out.println(status);
        String code = stockTakingCheck.getCode();
        stockTakingMapper.updateByCode(status,code);
        //将审核信息插入到审核表中
        int i = stockTakingCheckMapper.insert(stockTakingCheck);
        return i;
    }

    @Override
    public void addStockTaking(StockTakingVo stockTakingVo) {



    }
}
